﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Kugar.Core.Web;
using Kugar.Web.MVC;
using WS.FlowAuth.Web.Areas.Admin.Filters;
using WS.FlowAuth.Web.BLL;
using WS.FlowAuth.Web.Data.EF;

namespace WS.FlowAuth.Web.Areas.Admin.Controllers
{
    [UserAuthority]
    public class UserController : AdminBaseController
    {
        public ActionResult List()
        {
            var keyword = Request.GetString("keyword");
            var pageIndex = Request.GetInt("PageIndex", 1);
            
            
            ViewBag.Keyword = keyword;
            ViewBag.Data = WebBLL.User.GetUserList(keyword, pageIndex);

            return View();
        }

        [HttpGet]
        public ActionResult Detail()
        {
            var userID = Request.GetInt("UserID", -1);

            if (userID>0)
            {
                ViewBag.Data = WebBLL.User.GetUserByID(userID);
            }
            else
            {
                ViewBag.Data=new base_User();
            }

            return View();
        }

        [HttpPost,ActionName("Detail")]
        public ActionResult DetailPost()
        {
            var name = Request.GetString("UserName");
            var userID = Request.GetInt("UserID", -1);
            var password = Request.GetString("Password");

            if (string.IsNullOrWhiteSpace(name))
            {
                ModelState.AddModelError("UserName","用户名不能为空");
            }

            if (userID<=0 && string.IsNullOrWhiteSpace(password))
            {
                ModelState.AddModelError("Password","密码不能为空");
            }

            base_User data = null;
            
            if (ModelState.IsValid)
            {
                if (userID<=0)
                {
                    var ret = WebBLL.User.AddUser(name, password);

                    if (ret.IsSuccess)
                    {
                        MsgBoxAndCloseModal("添加成功");

                        data = WebBLL.User.GetUserByID(userID);
                    }
                    else
                    {
                        MsgBox("添加错误:" + ret.Message);
                    }
                }
                else
                {
                    data = WebBLL.User.GetUserByID(userID);
                }
            }

            if (data==null)
            {
                data=new base_User()
                {
                    UserName = name,
                    UserID = userID,
                    Password = password
                };
            }

            ViewBag.Data = data;

            return View();
        }

        [HttpPost]
        public ActionResult Method(string methodName)
        {
            var userID = Request.GetInt("UserID", -1);

            if (userID<=0)
            {
                return HttpNotFound();
            }

            switch (methodName)
            {
                case "delete":
                {
                    var ret=WebBLL.User.DeleteUser(userID);

                    return this.NewtonJson(ret);
                }
                case "reset":
                {
                    var ret = WebBLL.User.ResetUserPassword(userID, "123456");

                    return this.NewtonJson(ret);
                }
            }

            return HttpNotFound();
        }
    }
}